package com.czy.owner.adapter;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.czy.owner.R;
import com.czy.owner.entity.OrderInfoDetailModel;
import com.czy.owner.utils.MyLog;
import com.czy.owner.utils.glide.GlideUtils;

import java.util.List;

/**
 * Created by wangyang on 2017/6/5 0005.
 */

public class OrderDetailAdapter extends BaseRecyclerAdapter<OrderInfoDetailModel.OrderItemsBean, OrderDetailAdapter.OrderDetailViewHolder> {
    private OnItemReturnOrderListener onItemReturnOrderListener;
    private String orderStatus;
    private Context context;
    private String orderSubType;
    private double refundPayment; //退款
    private long nowTime;
    private long payTime;

    public OrderDetailAdapter(Context context, List<OrderInfoDetailModel.OrderItemsBean> data, String orderStatus, String orderSubType, long nowTime, long payTime) {
        super(context, data);
        this.context = context;
        this.orderStatus = orderStatus;
        this.orderSubType = orderSubType;
        this.nowTime = nowTime;
        this.payTime = payTime;
    }

    @Override
    public void onBindViewHolder(OrderDetailAdapter.OrderDetailViewHolder holder, int position) {
        super.onBindViewHolder(holder, position);
        holder.bindData(data.get(position));
    }

    @Override
    public OrderDetailAdapter.OrderDetailViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new OrderDetailAdapter.OrderDetailViewHolder(layoutInflater.inflate(R.layout.item_order_detail, parent, false));
    }

    class OrderDetailViewHolder extends BaseRecyclerAdapter.ViewHolder {
        private ImageView imgProduct;
        private TextView tvProductDescription;
        private TextView tvProductPrice;
        private TextView tvProductCount;
        private TextView tvOrderDetalRefund;
        private TextView tvSpac;
        private TextView tvMemberPrice;//会员价

        public OrderDetailViewHolder(View itemView) {
            super(itemView);
            imgProduct = (ImageView) itemView.findViewById(R.id.img_product);
            tvProductDescription = (TextView) itemView.findViewById(R.id.tv_product_description);
            tvProductPrice = (TextView) itemView.findViewById(R.id.tv_product_price);
            tvProductCount = (TextView) itemView.findViewById(R.id.tv_product_count);
            tvOrderDetalRefund = (TextView) itemView.findViewById(R.id.tv_order_detal_refund);
            tvSpac = (TextView) itemView.findViewById(R.id.tv_spac);
            tvMemberPrice = (TextView) itemView.findViewById(R.id.tv_member_price);
        }

        public void bindData(final OrderInfoDetailModel.OrderItemsBean data) {
            double originalPrice = data.getOriginalPrice();//原价
            double memberPrice = data.getPrice();//会员价
            if (originalPrice> memberPrice) {
                tvMemberPrice.setVisibility(View.VISIBLE);
                tvMemberPrice.setText("￥"+String.format("%.2f",memberPrice));
            }else {
                tvMemberPrice.setVisibility(View.GONE);
            }
            tvProductDescription.setText(data.getItemName());
            tvProductPrice.setText("￥" + String.format("%.2f",memberPrice));
            tvProductCount.setText("x" + data.getCount());
            if (TextUtils.isEmpty(data.getSpecValue())) {
                tvSpac.setVisibility(View.GONE);
            } else {
                if (data.getSpecValue().contains("规格:无规格")) {
                    tvSpac.setVisibility(View.GONE);
                } else {
                    tvSpac.setVisibility(View.VISIBLE);
                    tvSpac.setText(data.getSpecValue());
                }

            }
            GlideUtils.loadImage(context, data.getLogo(), imgProduct, new GlideUtils.ImageLoadListener<String, GlideDrawable>() {
                @Override
                public void onLoadingComplete(String uri, ImageView view, GlideDrawable resource) {

                }

                @Override
                public void onLoadingError(String source, Exception e) {

                }
            }, R.mipmap.default_icon, R.mipmap.default_icon);


            double price = data.getPrice() * data.getCount();
            double freight = data.getFreight();
            double servicePrice = data.getServicePrice();
            double discountPrice = data.getDiscountPrice();

            if (orderStatus.equals("waitDelivery")) {
                //待发货 可退商品+运费+安装费(如果有)
                refundPayment = price + freight + servicePrice - discountPrice;
            } else if (orderStatus.equals("delivery")) {
                //待收货-- 可退商品+安装费(如果有)，不可退运费
                refundPayment = price + servicePrice - discountPrice;
            } else if (orderStatus.equals("toInstall")) {
                //待安装--可退款/退货-可退商品+安装费，不可退运费
                refundPayment = price + servicePrice - discountPrice;
            } else if (orderStatus.equals("completed")) {
                //待评价--已安装的--无退款/退货按钮， 无安装的--可退款/退货，只能退商
                refundPayment = price - discountPrice;
            }


            long mTimeAfter = payTime + (60 * 60 * 24 * 1000 * 7) - nowTime;

            //待付款,已安装,已评价 ,已取消 无退款/退货按钮cancelled
            if (orderStatus.equals("waitPay") || orderStatus.equals("commented") || orderStatus.equals("cancelled") || refundPayment == 0.0 || orderSubType.equals("store") || mTimeAfter < 0) {
                tvOrderDetalRefund.setVisibility(View.GONE);
            } else {
                tvOrderDetalRefund.setVisibility(View.VISIBLE);
            }


            //卖家已安装的商品应不可退款退货
            if (data.getNeedService().equals("true") && data.getInstallStatus().equals("true")) {
                tvOrderDetalRefund.setVisibility(View.GONE);
            } else {
                if (data.getIsRefund().equals("false")) {
                    tvOrderDetalRefund.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            if (onItemReturnOrderListener != null) {
                                onItemReturnOrderListener.onSet(data, getAdapterPosition());
                            }
                        }
                    });
                } else {
                    if (data.getRefundsStatus().equals("applied")) {
                        tvOrderDetalRefund.setText("退款中");
                    } else if (data.getRefundsStatus().equals("complete")) {
                        tvOrderDetalRefund.setText("已退款");
                    }
                    tvOrderDetalRefund.setTextColor(context.getResources().getColor(R.color.appThemeSub2TitleColor));
                    tvOrderDetalRefund.setBackgroundResource(R.drawable.selector_order_gray);
                }
            }


        }

    }

    public interface OnItemReturnOrderListener {
        void onSet(OrderInfoDetailModel.OrderItemsBean data, int position);
    }

    public void setOnItemReturnOrderListener(OnItemReturnOrderListener onItemReturnOrderListener) {
        this.onItemReturnOrderListener = onItemReturnOrderListener;
    }
}